package com.obrock;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 18.09.12
 * Time: 10:48
 * To change this template use File | Settings | File Templates.
 */

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**
 * @author Rick
 */
public class ReportGraph extends Activity {

    TextView title;
    TextView accountAmount;
    TextView accountDebt;
    ImageView btnAction;
    TableLayout blockHeader;
    TableLayout container;

    Balance graph = null;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.graph);

        int bgColor = getResources().getColor(R.color.background_color);

        title = (TextView) findViewById(R.id.title);
        accountAmount = (TextView) findViewById(R.id.account_amount);
        accountDebt = (TextView) findViewById(R.id.account_debt);
        blockHeader = (TableLayout) findViewById(R.id.block_header);
        container = (TableLayout) findViewById(R.id.container);

        btnAction = (ImageView) findViewById(R.id.btnAction);
        btnAction.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        //Button btnCancel = (Button) findViewById(R.id.btnCancel);
        //btnCancel.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            graph = (Balance) extras.getSerializable("prognosis");
        }

        if(graph == null || graph.accountTo == null || graph.accountTo.accountID == -1) {
            finish();
            return;
        }

        int i = 0;
        double summary = graph.getInBalance(); //.accountTo.amount + graph.accountTo.paymentsFrom(null);
        double total = summary;
        double payed = 0.0;

        for (Balance transfer : graph.getGraph(null)) {

            summary -= transfer.amount;

            TextView date = Main.getTextView(this, Main.dateString(transfer.date));
            date.setTextSize(18);
            //date.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            if (transfer.approved) {
                payed += transfer.amount;
                date.setTextColor(getResources().getColor(R.color.approved));
            }

            TextView amount = Main.getTextView(this, String.format("%.2f", transfer.amount));
            amount.setGravity(Gravity.RIGHT);
            amount.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            amount.setTextSize(18);
            if (transfer.approved) {
                amount.setTextColor(getResources().getColor(R.color.approved));
            }

            TextView balance = Main.getTextView(this, String.format("%.2f", summary));
            balance.setTextSize(18);
            balance.setGravity(Gravity.RIGHT);
            if (transfer.approved) {
                balance.setTextColor(getResources().getColor(R.color.approved));
            }

            TableRow xrow = Main.addRow(this, bgColor, i++, new View[]{
                    date,
                    amount,
                    balance
            });

            container.addView(xrow);
        }

        if (graph.accountTo != null) {
            title.setText(graph.accountTo.name);
            accountAmount.setText(getResources().getString(R.string.prompt_credit_total, total));
            accountDebt.setText(getResources().getString(R.string.prompt_credit_payed, payed));
        }

    }
}
